<template>
	<view>

		<view class="form_box">
			<form @submit="formSubmit">
				<view class="formCon">
					<view class="formItem formFlex">
						<view class="label">联系人</view>
						<view class="controls">
							<input class="input" name="xingming" :value="addressDetail.xingming" placeholder='请输入联系人姓名'
								placeholder-class="placeholderColor"></input>
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">性别</view>
						<view class="controls">
							<u-radio-group v-model="sexValue" @change="sexChange">
								<u-radio v-for="(item, index) in sexList" :key="index" :name="item.id"
									active-color="#FED047">
									{{item.name}}
								</u-radio>
							</u-radio-group>
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">手机号码</view>
						<view class="controls">
							<input type="number" class="input" name="tel" :value="addressDetail.tel"
								placeholder='请输入手机号码' placeholder-class="placeholderColor" maxlength="11"></input>
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">收货地址</view>
						<view class="controls">
							<view class="pickerCon" @click="mapClick">
								<text v-if="province">{{province}}-{{city}}-{{area}}</text>
								<text class="placeholderColor" v-else>请选择收货地址</text>
								<u-icon name="arrow-right" color="#ccc" size="28"></u-icon>
							</view>
							<!-- <view class="pickerCon" @click="regionChange">
								<text v-if="province">{{province}}-{{city}}-{{area}}</text>
								<text class="placeholderColor" v-else>请选择地区</text>
								<u-icon name="arrow-right" color="#ccc" size="28"></u-icon>
							</view> -->
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">详细地址</view>
						<view class="controls">
							<input class="input" name="address" :value="address" placeholder='请输入详细地址'
								placeholder-class="placeholderColor"></input>
						</view>
					</view>
					<view class="formItem formFlex">
						<view class="label">设为默认</view>
						<view class="controls">
							<view class="pickerCon">
								<switch :checked="switchChecked" color="#FED047" @change="switchChange" />
							</view>
						</view>
					</view>
				</view>

				<!-- 底部固定 -->
				<view class="formPost_con">
					<button class="submitBtn" form-type="submit">保 存</button>
				</view>
			</form>
		</view>

		<!-- 省市区 -->
		<u-picker mode="region" v-model="regionShow" :area-code='["11", "1101", "110101"]' @confirm="regionConfirm">
		</u-picker>

	</view>
</template>

<script>
	export default {
		data() {
			return {
				// 地址id
				address_id: '',
				addressDetail: {},

				// 性别
				sexList: [{
						name: '先生',
						id: '1',
					},
					{
						name: '女士',
						id: '2',
					}
				],
				sexValue: '',

				// 省市区
				province: '',
				city: '',
				area: '',
				regionShow: false,
				longitude: '',
				latitude: '',
				address: '',

				// 设为默认
				switchChecked: true,
			}
		},
		onLoad(option) {
			uni.showLoading({
				title: '加载中'
			});
			this.address_id = option.address_id;
		},
		onShow() {},
		onReady() {
			this.getAddressDetail();
		},
		methods: {
			// 获取地址详情
			getAddressDetail() {
				var _this = this;
				_this.$u.get('/api/shopmulti.Address/get_details', {
					address_id: _this.address_id,
				}).then(res => {
					uni.hideLoading();
					console.log("==获取地址详情==");
					console.log(res);
					var addressDetail = res.data;
					_this.addressDetail = addressDetail;
					
					// 性别
					_this.sexValue = addressDetail.sex;

					// 省市区
					_this.province = addressDetail.province;
					_this.city = addressDetail.city;
					_this.area = addressDetail.country;
					_this.address = addressDetail.address;

					// 设为默认
					_this.switchChecked = addressDetail.is_default == 1 ? true : false;
				}).catch(res => {
					console.log(res);
				})
			},

			// 选择性别
			sexChange(e) {
				console.log(e);
				this.sexValue = e;
			},

			/**
			 * 选择省市区
			 */
			mapClick() {
				let _this = this;
				uni.chooseLocation({
					success: function(res) {
						console.log(res);
						var address = res.address;
						var reg = /.+?(省|市|自治区|自治州|县|区)/g;
						var addressArray = address.match(reg).toString().split(",");
						console.log(addressArray);

						_this.province = addressArray[0];
						_this.city = addressArray[1];
						_this.area = addressArray[2];
						_this.longitude = res.longitude;
						_this.latitude = res.latitude;
						_this.address = res.name;
					}
				});
			},
			// regionChange() {
			// 	this.regionShow = true;
			// },
			// regionConfirm(e) {
			// 	console.log(e);
			// 	this.province = e.province.label;
			// 	this.city = e.city.label;
			// 	this.area = e.area.label;
			// },

			// 设为默认
			switchChange(e) {
				console.log('设为默认', e.target.value);
				this.switchChecked = e.target.value;
			},

			// 新增地址
			formSubmit: function(e) {
				let _this = this;
				var formData = e.detail.value;

				if (!formData.xingming) {
					uni.showModal({
						content: '联系人不能为空',
						showCancel: false,
					})
					return;
				}

				if (!_this.sexValue) {
					uni.showModal({
						content: '请选择性别',
						showCancel: false,
					})
					return;
				}

				if (!formData.tel) {
					uni.showModal({
						content: '手机号码不能为空',
						showCancel: false,
					})
					return;
				} else if (_this.$u.test.mobile(formData.tel) == false) {
					uni.showModal({
						content: '请输入正确的手机号码',
						showCancel: false,
					})
					return;
				}

				console.log(_this.province)
				if (_this.province == '') {
					uni.showModal({
						content: '请选择地区',
						showCancel: false,
					})
					return;
				}

				if (!formData.address) {
					uni.showModal({
						content: '详细地址不能为空',
						showCancel: false,
					})
					return;
				}

				_this.$u.post('/api/shopmulti.Address/editone', {
					address_id: _this.address_id,
					xingming: formData.xingming,
					sex: _this.sexValue,
					tel: formData.tel,
					province: _this.province,
					city: _this.city,
					country: _this.area,
					longitude: _this.longitude,
					latitude: _this.latitude,
					address: formData.address,
					is_default: _this.switchChecked ? '1' : '0',
				}).then(res => {
					console.log("==新增地址==");
					console.log(res);

					uni.showToast({
						title: res.msg,
						duration: 2000
					});
					setTimeout(function() {
						uni.navigateBack({
							delta: 1
						})
					}, 2000)
				}).catch(res => {
					console.log(res);
				})
			},
		}
	}
</script>

<style scoped>
	.formPost_con {
		width: 100%;
		position: fixed;
		left: 0;
		bottom: 0;
	}
</style>
